﻿using MediatR;
using ProductionManagement.Application.Command.ProductionPlanCommand;
using ProductionManagement.Domain.Dtos.ProductionPlanDtos;
using ProductionManagement.Infrstructre.lnterface;

namespace ProductionManagement.Application.CommandHandler.ProductionPlanCommandHandler
{
    /// <summary>
    /// 反填生产计划
    /// </summary>
    public class ProductionPlanFanCommandHandler:IRequestHandler<ProductionPlanFanCommand, UpdProductionPlanDto>
    {
        private readonly IProductionPlanRepository _productionPlanRep;
        private readonly IProductionProductRepository _productionProductRep;

        public ProductionPlanFanCommandHandler(IProductionPlanRepository productionPlanRep, IProductionProductRepository productionProductRep)
        {
           this._productionPlanRep = productionPlanRep;
           this._productionProductRep = productionProductRep;
        }

        public async Task<UpdProductionPlanDto> Handle(ProductionPlanFanCommand request, CancellationToken cancellationToken)
        {
            var list = (from a in await _productionPlanRep.GetAll() select a).Where(t => t.ProductionPlansId == request.ProductionPlansId).ToList();
            var item = (from b in await _productionProductRep.GetAll() select b).ToList();
            var result = (from c in list
                          select new UpdProductionPlanDto
                          {
                              ProductionPlansId=c.ProductionPlansId,
                              PreProductionPlanId = c.PreProductionPlanId,
                              PreProductionState = c.PreProductionState,
                              PPDocumentNumber = c.PPDocumentNumber,
                              SaleContractId=c.SaleContractId,
                              PPlanName = c.PPlanName,
                              PPTotalQuantity = c.PPTotalQuantity,
                              Whetherautomatic = c.Whetherautomatic,
                              PPlanSmark = c.PPlanSmark,
                              UpdDto = (from d in item where d.PPDocumentNumber==c.PPDocumentNumber
                                        select new UpdProductionProductDto
                                        {
                                            ProductionProductId=d.ProductionProductId,
                                            PPDocumentNumber = d.PPDocumentNumber,
                                            ProductId = d.ProductId,
                                            PNumber = d.PNumber,
                                            PName = d.PName,
                                            PSpec = d.PSpec,
                                            PUnit = d.PUnit,
                                            PPNum = d.PPNum,
                                            MSubject = d.MSubject,
                                            PPAppStartDate = d.PPAppStartDate,
                                            PPAppEndDate = d.PPAppEndDate,
                                            PPReleDate = d.PPReleDate,
                                            PProuctSmark = d.PProuctSmark,
                                        }).ToList()
                          });
            return result.FirstOrDefault();
        }
    }
}
